<?php

session_start();

/**
 * @author Janosch Hirth
 * @copyright 2013
 */
echo("<script type='text/javascript' src='tcal.js'></script>\n");
echo("<link rel='stylesheet' type='text/css' href='tcal.css' />");
include 'nav.inc.php';
include 'dbConn.php';

html_head('Administration - Aufgabe bearbeiten');
echo "</head>\n<body>";
	if(!isset($_SESSION["username"])){					//kontrolliere ob Benutzer eingelogt ist
		echo("<div class='content' style='margin-left:350px'>");
	   echo ("Bitte erst <a href='../login.php'>einloggen</a>.");
	   exit;
	   echo("</div");
	}
	else
	{
		menu();
		echo("<div class='content' style='margin-left:350px'>");
		$aid = -1;
		if (isset($_REQUEST['AID']))
		{
			$aid = $_REQUEST['AID'];
		}
		if ($aid >-1)
		{
			$result = $dbAufgabe->get_Aufgaben_by_AID($aid);
			$task = mysql_fetch_array($result);
			$titel = $task['Titel'];
			$beschreibung = $task['Beschreibung'];
			$deadline = $task['Deadline'];
			$prioritaet = $task['Priorität'];
			$updateaid = $task['AID'];
		}
		if (isset($_REQUEST['next_page']))			//Seiten Variable
			$next_page = $_REQUEST['next_page'];
		else
			$next_page = '0';

		echo ("<form id='modTask' name='modTask' action='".$_SERVER['SCRIPT_NAME']."' method='post'>\n");	//Formular, zum eingeben der Daten
		if($next_page == '0'){
			echo "\t<fieldset>
					<legend> Wähle Aufgabe</legend>";

			echo "<select id='members' name='members' size=25 onchange='showTaskInfo();'>";

			$Aufgaben = $dbAufgabe->get_Aufgaben ();
			while ($row = mysql_fetch_array($Aufgaben))
			{
				echo "<option value='".$row['AID']."'>".$row['Titel']."</option>";
			}
			/*******************************/
			/*     Aufgabe ändern		   */
			/*******************************/
			echo "</select>
 				</fieldset>";

			//Titel, Beschreibung und Deadline ändern
			echo "\t<fieldset>
				<legend id = taskinfolegend style= 'display:none;'> Bearbeite Aufgabe</Legend>
				<ol id = 'taskinfo' style = 'display:none;' class='clearfix'>
					<li>
						<label for='titel'>Titel: </label>
						<input type = 'text' name = 'titel' id = 'titel' value='".$titel."'/>
					</li>
					<li>
						<label for='beschreibung'>Beschreibung: </label>
						<textarea cols='45' rows='4' name='content' id='content'>".$beschreibung."
						</textarea>
					</li>
					<li>
						<label for='deadline'>Deadline: </label>
						<input type = 'text' name = 'deadline' class='tcal' id = 'deadline' value='".$deadline."'/>
					</li>";
			
			//Priorität ändern
			echo	"<li>";
			echo "Priorität:</br>";
			echo ("\n<select name='prioritaet' size='1'>\n");
			$prioritaetstufen = array('0','1','2','3','4','5','6','7','8','9','10');
			$max = sizeof($prioritaetstufen);
			echo ("<option value='NULL'  selected='selected'>$prioritaet</option>\n");
			for($zaehler = 0; $zaehler < $max; $zaehler++)
			{
					echo ("<option value='".$prioritaetstufen[$zaehler]."'>".$prioritaetstufen[$zaehler]."</option>\n");
			}
			echo ("</select><br/>\n");
			echo"</li>";

			//Soll bearbeitet werden von ändern
			$result = mysql_query("SELECT NID FROM task_istGedachtFor WHERE AID = '".$updateaid."'");
			$storeArray = array();
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
			{
  				$storeArray[] =  $row['NID'];  
			}
			$result2 = $dbAufgabe->get_X_of_mitarbeiter('*');
			echo	"<li>";
			echo "Soll bearbeitet werden von:<br/>";
			$result3 = $dbAufgabe->get_X_of_current_work_by_Y ("NID", "AID = '".$updateaid."'");
			$current = mysql_fetch_assoc($result3);

			while ($row = mysql_fetch_assoc($result2))
			{
				if (in_array($row['m_id'], $storeArray))
				{
					if (in_array($row['m_id'], $current))
					{
						echo ("<input type='checkbox' name='neu[]' checked = true value='".$row['m_id']."'>".$row['m_kurz']." -*aktiv*-<br>");
					}
					else
					{
						echo ("<input type='checkbox' name='neu[]' checked = true value='".$row['m_id']."'>".$row['m_kurz']."<br>");	
					}
				}
				else
				{
					echo ("<input type='checkbox' name='neu[]' value='".$row['m_id']."'>".$row['m_kurz']."<br>");	
				}
			}

			//Speicherung der AID für später
			echo "<input type ='hidden' name='currentAID' id='currentAID' value='".$updateaid."'/>";
			echo "<input type='hidden' name='AID' id='AID' value='-1' />";		
			echo "<input type='hidden' name='INDEX' id='INDEX' value='-1' />";		
			echo " 	<li class='last'>
      					<input type='submit' name='submitBTN' id='submitBTN' value='Eintragen' onclick='this.form.force.value=\"0\"' />
    				</li>
  				</ol>
  				</fieldset>";
			echo "\t</form>\n";
			if ($aid >-1) 
			{
				echo "<script type='text/javascript'>showNowForReal('".$_REQUEST["INDEX"]."');</script>";
			}
			if (isset($_REQUEST['submitBTN']) && isset($_REQUEST['currentAID']))
			{
				$new = array();
				$old = array();
				$AID = $_REQUEST['currentAID'];
				$update = $dbAufgabe->UPDATE_AUFGABE_by_AID ($AID, $_POST['content'], $_POST['deadline'], $_POST['titel'], $_POST['prioritaet']);
				$result = mysql_query("SELECT NID FROM task_istGedachtFor WHERE AID = '".$AID."'");
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
				{
  					$old[] = $row['NID'];  
				}
				$result = $dbAufgabe->get_X_of_mitarbeiter('m_kurz, m_id');
// ALLE AUFGABEN HOLEN; DIE BISHER AKZEPTIERT WURDEN => accArray
//define boolean variable = false
				$result = $dbAufgabe->get_X_of_current_work_by_Y ("NID", 'AID = '.$AID.'');
				$accArray = array();
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					$accArray [] = $row['NID'];
				}
				$pruefer = false;
				$delterly = false;

				if (isset($_POST['neu']))
				{
					$addArray = array();
					$delArray = array();
					$new = $_POST['neu'];


					foreach ($new as $check)
					{
						if (!in_array($check, $old))
							array_push($addArray, $check);
					}
					
					foreach ($old as $oldcheck)
					{
						// if is in accarray then booleanvariable = true
						if (!in_array($oldcheck, $new))
						{							
							array_push($delArray, $oldcheck);
						}						
					}

					foreach ($delArray as $active)
					{
						if (in_array($active, $accArray))
						{
							$pruefer = true;
							$worker [] = $oldcheck;
						}
					}
					// if booleanvariable then DIALO ÖFFNEN USW
					if ($pruefer == true)
					{
					echo "<script language=javascript>
							var sName=confirm('Achtung: \\n\\nSie versuchen Personen, die die Aufgabe schon angenommen haben von ihr abzuziehen');
					</script>";
					}
					if (isset($_POST['sName']))
					{
						if ($_POST['sName'] == 1)
							$deleterly = true;
					}
					if ($deleterly && $pruefer || !$pruefer )
					{
						foreach ($addArray as $mid)
							$dbAufgabe->INSERT_INTO_istGedachtFor($AID, $mid);						

						foreach ($delArray as $mid)
							$dbAufgabe->DELETE_From_istGedachtFor_by_AID_NID ($AID, $mid);

						foreach ($worker as $mid)
							$dbAufgabe->DELETE_FROM_wirdBearbeitetVon_X_Y ($mid, $AID);
					}// ENDELESE
				}
			}
		}
	}

echo "</div>";
echo "</body>";
?>
